package com.nnggstory.logs;

import java.net.Socket;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.nnggstory.logs.logsocket.LoggerSocketServer;
import com.nnggstory.logs.webserver.SimpleWebServer;
import com.nnggstory.logs.websocket.WebSocketPushServer;

public class Main {
	public static void main(String[] args) {
		Map<String, Object> serverContext = new HashMap<String, Object>();
		WebSocketPushServer ws = null;
		LoggerSocketServer lss = null;
		SimpleWebServer sws = null;
		
		if(args.length != 0 && args.length != 3 && args.length != 4) {
			System.out.println("parameters count is wrong");
			return;
		}
		if(args.length == 0) {
			ws = new WebSocketPushServer();
			lss = new LoggerSocketServer();
			sws = new SimpleWebServer();
		} else if(args.length == 3 || args.length == 4) {
			try {
				lss = new LoggerSocketServer(Integer.parseInt(args[0]));
				ws = new WebSocketPushServer(Integer.parseInt(args[1]));
				sws = new SimpleWebServer(Integer.parseInt(args[2]));
			} catch (Exception e) {
				System.out.println("parameters type is wrong\n 1,2,3 parameter type must be integer");
				return;
			}
			if(args.length == 4) {
				serverContext.put("hostName", args[3]);
			}
		}
		
		Set<Socket> loggerSocketSet = new HashSet<Socket>();
		
		serverContext.put("WebSocketServer", ws);
		serverContext.put("LoggerSocketServer", lss);
		serverContext.put("SimpleWebServer", sws);
		serverContext.put("loggerClients", loggerSocketSet);
		
		ws.setContext(serverContext);
		lss.setContext(serverContext);
		sws.setContext(serverContext);
		
		lss.serverStart();
		System.out.println("LoggerSocketServer is run...");
		ws.serverStart();
		System.out.println("WebSocketServer is run...");
		sws.serverStart();
		System.out.println("SimpleWebServer is run...");
	}
}